import 'network_utils.dart';

/// 响应码
enum ResponseCode {
  /// 未知
  unknow(""),

  /// 成功
  success("00000"),
  specialSuccess("200"),

  /// token 失效
  invalidToken("401");

  /// 响应码
  final String code;
  const ResponseCode(this.code);

  /// 通过code获取枚举
  static ResponseCode rawValue(String code) => ResponseCode.values.firstWhere(
        (element) => element.code == code,
        orElse: () => ResponseCode.unknow,
      );

  /// token失效？
  bool get isTokenExpire {
    switch (this) {
      case ResponseCode.invalidToken:
        return true;
      default:
        return false;
    }
  }

  /// 处理消息
  String? handleMessage(String? msg) {
    return msg;
  }

  /// 发送事件
  onEvent(String? message, dynamic data) {
    eventBus.fire(NetworkEvent(
      code: this,
      message: message,
      data: data,
    ));
  }
}

/// 网络事件
class NetworkEvent {
  final ResponseCode code;
  final String? message;
  final dynamic data;

  NetworkEvent({required this.code, this.message, this.data});
}
